package cn.itrip.auth.controller;

import cn.itrip.auth.service.TokenService;
import cn.itrip.auth.service.UserLogService;
import cn.itrip.dto.Dto;
import cn.itrip.pojo.ItripUser;
import cn.itrip.utlis.DtoUtils;
import cn.itrip.utlis.ErrorCode;
import cn.itrip.vo.ItripTokenVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

/**
 * 登录，注销控制器
 */
@Controller
@RequestMapping("/api")
public class LoginController {

    @Resource
    private UserLogService userLogService;
    @Resource
    private TokenService tokenService;

    /**
     * 用户登录
     * @param name
     * @param password
     * @param request
     * @return
     */
    @ApiOperation(value="用户登录实现",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="用户登录")
    @RequestMapping(value = "/dologin", method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto login(@RequestParam String name,@RequestParam String password, HttpServletRequest request){
        /**
         * 验证用户信息
         */
       ItripUser itripUser=null;
        try {
            itripUser=userLogService.userLogin(name,password);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtils.returnFail(e.getMessage(),"444");
        }
        /**
         * 生成token
         */
        String token = null;
        try {
            token = tokenService.createToken(request.getHeader("user-agent"),itripUser);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtils.returnFail(e.getMessage(),"445");
        }
        /**
         * 保存-token,并构造一个tokenVo返回！
         */
        tokenService.save(token,itripUser);
        ItripTokenVO tokenVO=new ItripTokenVO(token,
                Calendar.getInstance().getTimeInMillis()+TokenService.SESSION_TIMEOUT*1000,
                Calendar.getInstance().getTimeInMillis());
        return DtoUtils.returnDataSuccess(tokenVO);
    }

    /**
     * 注销
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value="用户注销实现",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="用户注销")
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    @ResponseBody
    public Dto logout(HttpServletRequest request) throws Exception {
        String token=request.getHeader("token");
        if (tokenService.exitToken(request.getHeader("user-agent"),token))
        {
            try {
                tokenService.logout(token);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtils.returnFail("注销失败",ErrorCode.AUTH_TOKEN_INVALID);
            }
            return DtoUtils.returnDataSuccess("注销成功");
        }
        return DtoUtils.returnFail("注销失败",ErrorCode.AUTH_TOKEN_INVALID);
    }
}
